import scrapy
from bossPro.items import BossproItem


class BossSpider(scrapy.Spider):
    name = "boss"
    # allowed_domains = ["www.xxx.com"]
    start_urls = ["https://www.zhipin.com/web/geek/job?query=Java&city=101280700"]

    url = 'https://www.zhipin.com/web/geek/job?query=Java&city=101280700&page=1'
    page_num = 2

    # 回调函数接受item
    def parse_detail(self, response):
        item = response.meta['item']

        job_desc = response.xpath('')
        job_desc = ''.join(job_desc)
        # print(job_desc)
        item['job_desc'] = job_desc

        yield item

    # 解析首页中的岗位名称
    def parse(self, response):
        li_list = response.xpath('')
        for li in li_list:
            item = BossproItem()

            job_name = li.xpath('.//div[@class="').extract_first()
            item['job_name'] = job_name
            # print(job_name)
            detail_url = li.xpath('').extract_first()
            # 对详情页发请求获取详情页的页面源码数据
            # 手动请求的发送
            # 请求传参：meta={}，可以将meta字典传递给请求对应的回调函数
            yield scrapy.Request(detail_url, callback=self.parse_detail, meta={'item': item})

        # 分页操作
        if self.page_num <= 3:
            new_url = format(self.url % self.page_num)
            self.page_num += 1
            yield scrapy.Request(new_url, callback=self.parse)
